package com.ufo.store.management.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ufo.store.management.R;
import com.ufo.store.management.utils.GlideUtils;
import com.ufo.store.management.utils.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *  作者：taoli on 2017/7/27 0027 15:57
 *  
 */
public class Isszmbalv3StockTypeRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context = null;
    private List<String> mNameLists = new ArrayList<>();
    private List<String> mIndexLists = new ArrayList<>();
    private Map<String, String> mStockTypeMap = new HashMap<>();
    private OnClickListener mListener = null;

    public Isszmbalv3StockTypeRecyclerViewAdapter(Context context, Map<String, String> map) {
        this.context = context;
        setMap(map);
    }

    //升序排序
    private void setLists() {
        List<Map.Entry<String, String>> list = new ArrayList<>(mStockTypeMap.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                int x = Integer.parseInt(o1.getKey().trim());
                int y = Integer.parseInt(o2.getKey().trim());
                return x - y;
            }
        });
        for (Map.Entry<String, String> entry : list) {
            mNameLists.add(entry.getValue().trim());
            mIndexLists.add(entry.getKey().trim());
        }
    }

    public String getIndexItem(int pos) {
        return mIndexLists.get(pos);
    }

    public String getNameItem(int pos) {
        return mNameLists.get(pos);
    }

    public void setMap(Map<String, String> map) {
        if( map != null && map.size() > 0) {
            this.mStockTypeMap.clear();
            this.mStockTypeMap.putAll(map);
            Iterator<Map.Entry<String, String>> it = this.mStockTypeMap.entrySet().iterator();
            while(it.hasNext()){
                Map.Entry<String, String> entry = it.next();
                try {
                    int index = Integer.parseInt(entry.getKey().trim());
                    if( index == -1 ){
                        it.remove();
                    }
                }catch(Exception e){
                    it.remove();
                }
            }
            this.mNameLists.clear();
            setLists();
        }
    }

    public Map<String, String> getMap() {
        return mStockTypeMap;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        return mNameLists.size();
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {
        TextView nameTv;
        ImageView bgIv;
        public RecyclerViewHolder(View view) {
            super(view);
            nameTv = (TextView) view.findViewById(R.id.name_tv);
            bgIv = (ImageView) view.findViewById(R.id.bg_iv);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == 1) {
            holder = new RecyclerViewHolder(LayoutInflater.from(
                    this.context).inflate(R.layout.stock_type_item, parent,
                    false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final String info = mNameLists.get(position);
        if (info != null) {
            if (holder instanceof RecyclerViewHolder) {
                RecyclerViewHolder holder1 = (RecyclerViewHolder) holder;
                holder1.nameTv.setText(info);
                int resource_ID = context.getResources().getIdentifier("type" + String.valueOf(position +1), "drawable", Utils.getPackageName(context));
                //ImageLoader.getInstance().displayImage("drawable://" + resource_ID, holder1.bgIv);
                GlideUtils.LoadImageWithResource(context,resource_ID, holder1.bgIv );
            }
        }
        //点击事件
        if (mListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建两个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getPosition();
                        mListener.onItemClick(getIndexItem(pos), getNameItem(pos));
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = holder.getPosition();
                        mListener.onItemLongClick(pos);
                        return true;
                    }
                });
            }
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position, List payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        }
    }

    public void setOnClickListener(OnClickListener l) {
        mListener = l;
    }

    public interface OnClickListener {
        void onItemClick(String index, String name);

        void onItemLongClick( int position);
    }
}
